import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class Solution {
    public static void merge(int[] nums1, int m, int[] nums2, int n) {
        for (int i = 0; i < n; i++) {
            nums1[m+i] = nums2[i];
        }
        Arrays.sort(nums1);
        //为什么能直接使用Arrays中的方法？Array和Arrays以及ArrayList之间有什么区别？
        System.out.println(Arrays.toString(nums1));
    }

    public static void merge1(int[] nums1, int m, int[] nums2, int n) {
        int[] sorted = new int[m+n];
        int cur = 0;
        int p1 = 0,p2 = 0;
        while (p1 < m || p2 < n){
            if (p1 == m){
                cur = nums2[p2];
                p2++;
            }else if (p2 == n){
                cur = nums1[p1];
                p1++;
            }else if (nums1[p1] > nums2[p2]){
                cur = nums2[p2];
                p2++;
            }else {
                cur = nums1[p1];
                p1++;
            }
            sorted[p1 + p2 - 1] = cur;
        }
        for (int i = 0; i < m+n; i++) {
            nums1[i] = sorted[i];
        }
        System.out.println(Arrays.toString(nums1));
    }

    public static void main(String[] args) {
        int[] nums1 = {1,2,6,0,0,0};
        int[] nums2 = {3,5,7};
        int m = 3,n = 3;
        Solution.merge1(nums1,m,nums2,n);
    }
}